package com.tianqu.tms.www.bid.controller;

import java.util.Date;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.AucCarInfo;
import com.tianqu.tms.entity.AucManagement;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.www.bid.viewhelper.BidCarSerchInputViewHelper;

public class BidCarSerchAllDetailController extends TmsController {
	private static String SERIAL = "serial";
	private static String ID = "id";
	private static String FLG = "flg";
	private Logger logger = Logger.getLogger(BidCarSerchAllDetailController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			String serial = (String) uc.getAttribute(SERIAL);
			String id = (String) uc.getAttribute(ID);
			String flg = "";
			if (!StringUtil.isNullOrBlank((String)uc.getAttribute(FLG))) {
				flg = (String)uc.getAttribute(FLG);
				uc.removeAttribute(FLG);
			} else {
				flg = (String) uc.getParameter(FLG);
			}
 
			// 获得字典表信息
			Codes code = Codes.getInstance();
			// 车辆详细信息
			AucCarInfo auc = AucCarInfo.getInstance();
			auc = auc.getaucCarBase(serial);
			BidCarSerchInputViewHelper view = viewHelper(entityToView(auc), code);
			// 获得竞价信息
			AucManagement aucmana = AucManagement.getInstance();
			aucmana = aucmana.getAucInfoBySerialAll(Integer.valueOf(id));
			// 竞价ID
			view.setId(aucmana.getId());
			// 竞价状态
			view.setBidStatus(String.valueOf(aucmana.getBidStatus()));
			// 迁移元判断用FLG
			view.setFlg(flg);
			// 图片详细信息 
			view.setImages1(auc.getPhoto1());
			view.setImages2(auc.getPhoto2());
			view.setImages3(auc.getPhoto3());
			view.setImages4(auc.getPhoto4());
			view.setImages5(auc.getPhoto5());
			view.setImages6(auc.getPhoto6());
			view.setImages7(auc.getPhoto7());
			view.setImages8(auc.getPhoto8());
			view.setImages9(auc.getPhoto9());
			view.setImages10(auc.getPhoto10());
			view.setImages11(auc.getPhoto11());
			view.setImages12(auc.getPhoto12());
			view.setImages13(auc.getPhoto13());
			view.setImages14(auc.getPhoto14());
			view.setImages15(auc.getPhoto15());
			view.setImages16(auc.getPhoto16());
			view.setImages17(auc.getPhoto17());
			view.setImages18(auc.getPhoto18());
			view.setImages19(auc.getPhoto19());
			view.setImages20(auc.getPhoto20());
			view.setImages21(auc.getPhoto21());
			view.setImages22(auc.getPhoto22());// 图片从8张增加到20张  modify by mufeiyu
			
			// 当前时间
			Date nowDate = new Date();

			if ((aucmana.getStartTime().getTime() - nowDate.getTime()) / 60000 >= 10) {
				view.setModifyFlg("1");
			} else {
				view.setModifyFlg("0");
			}
			
			uc.setAttribute(TmsConstants.VIEWHELP, view);
			logger.debug(this.getClass().getName() + "#execute was completed.");
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
	}

	/**
	 * entity转换view
	 * 
	 * @param auc
	 * @return
	 */
	private BidCarSerchInputViewHelper entityToView(AucCarInfo auc) {
		BidCarSerchInputViewHelper vie = new BidCarSerchInputViewHelper();
		// 基本 信息
		vie.setSerial(auc.getSerial());
		vie.setCarName(auc.getCarName());
		vie.setVin(auc.getVin());
		vie.setLicensePlate(auc.getLicensePlate());
		vie.setMileage(StringUtil.getString(auc.getMileage()));
		vie.setPrice(StringUtil.getString(auc.getPrice()));
		vie.setColor(StringUtil.getString(auc.getColor()));
		vie.setRegDate(auc.getRegDate());
		vie.setJqx(auc.getJqx());
		vie.setYearCarefulPeriod(auc.getYearCarefulPeriod());
		vie.setContactPerson(auc.getContactPerson());
		vie.setContactPhone(auc.getContactPhone());
		// 车主信息
		vie.setCarOwner(auc.getCarOwner());
		vie.setOwnerType(StringUtil.getString(auc.getOwnerType()));
		vie.setCarOwnerIdCardNo(auc.getCarOwnerIdCardNo());
		vie.setOwnerSex(auc.getOwnerSex());
		vie.setOwnerPhone(auc.getOwnerPhone());
		vie.setOwnerAddress(auc.getOwnerAddress());
		vie.setOwnerBirthday(auc.getOwnerBirthday());		

		return vie;
	}

	/**
	 * 获得车辆属性
	 * 
	 * @param view
	 * @param code
	 * @param carStyle
	 * @return
	 */
	private BidCarSerchInputViewHelper viewHelper(BidCarSerchInputViewHelper view,
			Codes code) {

		if (!StringUtil.isNullOrBlank(view.getColor())) {
			// 颜色
			view.setColor(code.getItemValueByItemIdAndItemCode(
					Codes.VEHICLE_COLOR, Integer.parseInt(view.getColor())));
		}
		if (!StringUtil.isNullOrBlank(view.getOwnerType())) {
			// 车主类型
			view.setOwnerType(code.getItemValueByItemIdAndItemCode(
					Codes.CAR_OWNER, Integer.parseInt(view.getOwnerType())));
		}
		return view;
	}
}
